package xyz.litecoder.demos;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * 暂未使用
 * 
 * @author zhugefubin
 * 
 */
@Deprecated
public class ZImageView extends View {
	private Paint mPaint;// 绘图画笔
	private Bitmap cachebBitmap;// 手写缓存图层
	private Canvas mCanvas;// 手写缓存画布
	private Path mPath;// 手写路径
	private Paint cachebBitmapPaint;// 缓存画笔
	private float strokeWidth = 3;// 默认线条宽度
	private ExecutorService taskPool;
	private boolean isScollMode = false;

	/**
	 * @return isScollMode
	 * @字段类型 boolean
	 */
	public boolean isScollMode() {
		return isScollMode;
	}

	@SuppressLint("NewApi")
	public ZImageView(Context context, AttributeSet attrs, int defStyleAttr,
			int defStyleRes) {
		super(context, attrs, defStyleAttr, defStyleRes);
		init();
	}

	public ZImageView(Context context, AttributeSet attrs, int defStyleAttr) {

		super(context, attrs, defStyleAttr);
		init();
	}

	public ZImageView(Context context, AttributeSet attrs) {

		super(context, attrs);
		init();
	}

	public ZImageView(Context context) {
		super(context);
		init();
	}

	/**
	 * @return handWritingLayer
	 * @字段类型 Bitmap
	 */
	public Bitmap getHandWritingLayer() {

		return cachebBitmap;
	}

	/**
	 * @param handWritingLayer
	 *            要设置的 handWritingLayer
	 * @字段类型 Bitmap
	 */
	public void setHandWritingLayer(Bitmap handWritingLayer) {
		this.cachebBitmap = handWritingLayer;
		invalidate();
	}

	public void init() {
		mPath = new Path();
		cachebBitmapPaint = new Paint(Paint.DITHER_FLAG);
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setDither(true);
		mPaint.setColor(0xFFFF0000);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(strokeWidth);
		setBackgroundResource(R.drawable.p);
		taskPool = Executors.newFixedThreadPool(3);
		invalidate();
	}

	/**
	 * @param color
	 * @说明
	 */
	public void setColor(int color) {
		mPaint.setColor(color);
	}

	public int getColor() {
		return mPaint.getColor();
	}

	/**
	 * @return strokeWidth
	 * @字段类型 float
	 */
	public float getStrokeWidth() {

		return strokeWidth;
	}

	/**
	 * @param strokeWidth
	 *            要设置的 strokeWidth
	 * @字段类型 float
	 */
	public void setStrokeWidth(float strokeWidth) {
		this.strokeWidth = strokeWidth;
	}

	/**
	 * @说明 擦除模式
	 */
	public void clearMode() {
		mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
		mPaint.setStrokeWidth(30);
		// mPaint.setColor(Color.TRANSPARENT);
		isScollMode = false;
	}

	/**
	 * @绘画模式
	 */
	public void drawMode() {
		mPaint.setXfermode(null);
		mPaint.setAlpha(0xFF);
		mPaint.setStrokeWidth(strokeWidth);
		mPaint.setColor(Color.RED);
		isScollMode = false;
	}

	/**
	 * @绘画模式
	 */
	public void scrollMode() {
		isScollMode = true;
	}

	/**
	 * @说明
	 */
	private void createCachebBitmap() {
		if (cachebBitmap == null || cachebBitmap.isRecycled()) {
			cachebBitmap = Bitmap.createBitmap(getWidth(), getHeight(),
					Bitmap.Config.ARGB_8888);
			mCanvas = new Canvas(cachebBitmap);
			try {
				mCanvas.drawBitmap(
						BitmapFactory.decodeFile("/sdcard/temp/drawTemp.png"),
						0, 0, null);
			} catch (Exception e) {
				Log.d(VIEW_LOG_TAG, "没发现临时文件");
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (isScollMode) {
			Log.d(VIEW_LOG_TAG, "滚动模式,跳过绘图");
			return;
		}
		canvas.drawPath(mPath, mPaint);
		cachebBitmapPaint.setColor(mPaint.getColor());
		if(cachebBitmap==null){
			createCachebBitmap();
		}
		canvas.drawBitmap(cachebBitmap, 0, 0, cachebBitmapPaint);
		taskPool.execute(new SaveBitmapThread());
	}

	class SaveBitmapThread extends Thread {
		@Override
		public void run() {
			mCanvas.save(Canvas.ALL_SAVE_FLAG);
			mCanvas.restore();
			Log.d(VIEW_LOG_TAG, "mCanvas.save()");
		}
	}

	// private static final float TOUCH_TOLERANCE = 4;

	private void touch_start(float x, float y) {
		mPath.reset();
		mPath.moveTo(x, y);
		mCanvas.drawPath(mPath, mPaint);
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		System.out.println("x:" + event.getX() + "\ty:" + event.getY());
		if (isScollMode) {
			return false;
		} else {
			float x = event.getX();
			float y = event.getY();
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				touch_start(x, y);
				invalidate();
				return true;
			case MotionEvent.ACTION_MOVE:
				touch_move(x, y);
				invalidate();
				return true;
			case MotionEvent.ACTION_UP:
				touch_up(x, y);
				invalidate();
				return true;
			}
		}
		return true;
	}

	private void touch_move(float x, float y) {
		// float dx = Math.abs(x - mX);
		// float dy = Math.abs(y - mY);
		// if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
		// mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
		// mX = x;
		// mY = y;
		// }
		mPath.lineTo(x, y);
		mCanvas.drawPath(mPath, mPaint);
	}

	private void touch_up(float x, float y) {
		mPath.lineTo(x, y);
		// commit the path to our offscreen
		mCanvas.drawPath(mPath, mPaint);
		// kill this so we don't double draw
		mPath.reset();
	}

	public void saveToFile(String filename) throws FileNotFoundException {
		File f = new File(filename);
		// if (f.exists())
		// throw new RuntimeException("文件：" + filename + " 已存在！");

		FileOutputStream fos = new FileOutputStream(new File(filename));
		// 将 bitmap 压缩成其他格式的图片数据
		cachebBitmap.compress(CompressFormat.PNG, 100, fos);
		try {
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}